/**
 * Created by 000188 on 2016/6/27.
 */
import React from 'react';
import Ajax from 'superagent';
var Content = require('content');
var verify = require('verify');
var Btn = require('btn');
var LuckDraw = require('luck-draw');
var Logo = require('logo');
var Msg = require('msg');
var EndAct = require('end-act');

var MobileForm = React.createClass({
    render(){
        return <div className="mobile-form">
            <Logo />
            <div className="mobile-form-box">
                <label>
                    <span>输入手机号</span>
                    <input ref="phone" style={{width:'60%'}} type="text"/>
                </label>
                <label>
                    <span>短信验证码</span>
                    <input ref="message" type="text"/>
                    <button ref="code" onClick={this.getCode}>获取验证码</button>
                </label>
                <label>
                    <span>动态验证码</span>
                    <input ref="dynamic" type="text"/>
                    <img ref="dynamicImg" onClick={this.updateValidateCode}/>
                </label>
                <Btn width="100%" text="去试试手气" click={this.toScene}/>
            </div>
        </div>
    },
    toScene(){
        let phone = this.refs.phone;
        let message = this.refs.message;
        let dynamic = this.refs.dynamic;
        if (verify.phone(phone.value)) {
            Msg.alert('手机号错误');
            return
        }
        if (message.value.length < 6) {
            Msg.alert('短信验证码必须为6位')
            return
        }
        if (dynamic.value.length < 4) {
            Msg.alert('动态验证码必须为5位')
            return
        }
        if (this.start) {
            return
        }
        this.start = true;
        require(['config'], Config=> {
            Ajax.post(Config.api.user)
                .set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
                .send({
                    type: 2,
                    phone: phone.value,
                    code: message.value,
                    authcode: dynamic.value
                })
                .end((err, d)=> {
                    if(!this.start){
                        return;
                    }
                    this.start = false;
                    let data = JSON.parse(d.text);
                    if (err) {
                        return
                    }
                    if (data.SuccessCode == '0') {
                        switch (data.Code) {
                            case '0':
                                Content.render(<LuckDraw />);
                                break;
                            case '1':
                                Content.render(<EndAct />);
                        }
                    } else {
                        Msg.alert(data.Message);
                    }
                })
        })
    },
    start: false,
    updateValidateCode(){
        var dynamicImg = this.refs.dynamicImg;
        require(['config'], Config=> {
            dynamicImg.src =  Config.api.ValidateCode + '?' + new Date().getTime();
        });
    },
    componentDidMount(){
        this.updateValidateCode();
    },
    getCode(){
        let code = this.refs.code;
        let phone = this.refs.phone;
        if (code.disabled) {
            return;
        }
        code.disabled = true;
        require(['config'], Config=> {
            Ajax.post(Config.api.user)
                .set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
                .send({
                    type: 1,
                    phone: phone.value
                }).end((err, d)=> {
                if (err) {
                    console.log(err)
                    return
                }
                let data = JSON.parse(d.text);
                switch (data.SuccessCode) {
                    case '0':
                        let text = code.innerHTML;
                        let Time = 60000;
                        code.classList.add('act');
                        Msg.alert(data.Message);
                        let setInte = setInterval((function dao() {
                            Time -= 1000;
                            code.innerHTML = Time / 1000 + '秒后可重发短信';
                            if (Time <= 0) {
                                clearInterval(setInte);
                                code.innerHTML = text;
                                code.disabled = false;
                                code.classList.remove('act')
                            }
                            return dao;
                        })(), 1000);
                        break;
                    default:
                        code.disabled = false;
                        Msg.alert(data.Message);
                }
            })
        });
    }
})

module.exports = MobileForm;